package images;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GameControl extends JPanel {
        private GameDisplay display;
        private KeyInput inputsystem;
        private GameEngine engine;
        private PlayerCharacter player;

        public enum GameState {Menu, Game, Text};
        public GameState state;

        //testing variable
        Keys[] test;

        private int xmov = 0, ymov = 0;


        public GameControl() {
                display = new GameDisplay(this);
                inputsystem = new KeyInput(this);
                engine = new GameEngine(this);
                player = new PlayerCharacter(this);
                state = GameState.Game;

                test = new Keys[2];
                test[0] = Keys.ERROR;
                test[1] = Keys.ERROR;

                Timer t = new Timer(30, new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							repaint();
						}
					});


				t.start();

        }
        public void update(Keys[] k) {
                test = k;
                if (state == GameState.Game) {
						if (k[0] == Keys.UP) {
									ymov = -2;
									xmov = 0;
						} else if (k[0] == Keys.DOWN) {
									ymov = 2;
									xmov = 0;
						} else if (k[0] == Keys.LEFT) {
									xmov =-2;
									ymov = 0;
						} else if (k[0] == Keys.RIGHT) {
									xmov = 2;
									ymov = 0;
						} else if (k[0] == Keys.D_DL) {
									xmov = -2;
									ymov = 2;
						} else if (k[0] == Keys.D_UL) {
									xmov = -2;
									ymov = -2;
						} else if (k[0] == Keys.D_UR) {
									xmov = 2;
									ymov = -2;
						} else if (k[0] == Keys.D_DR) {
									xmov = 2;
									ymov = 2;
						} else {
									xmov = 0;
									ymov = 0;
						}
				}



				if (k[1] == Keys.BUTTON1) {
					if (state == GameState.Text) {
						state = GameState.Game;
						repaint();
						return;
					}
					if (((player.getX() > 650 && player.getX() < 700) || (player.getX() + 50 > 650 && player.getX() + 50 < 700)) && ((player.getY() + 50 > 50 && player.getY() + 50 < 100) || (player.getY() < 100 && player.getY() > 50))) {
						state = GameState.Text;
						xmov = 0;
						ymov = 0;
						inputsystem.reset();
					}
				}
                repaint();
        }
        public void update() {

        }
        public void paintComponent(Graphics g) {
                //super.paintComponent(g);
				//Graphics2D g2d = (Graphics2D) g;
                display.draw(g);

               	if (state == GameState.Text) {
					g.setColor(Color.white);
					g.fillRect(100, 500, 600, 50);
					g.setColor(Color.blue);
					g.drawRect(100, 500, 600, 50);
					g.drawString("success", 375, 525);
				}
                player.move(xmov, ymov);
                player.draw(g);
				g.setColor(Color.red);
				g.drawRect(650, 50, 50, 50);
				g.setColor(Color.black);


                //g.drawString("test: " + test, x, y);
                //g.drawString("test1: " + test[0] + " test2: " + test[1], 50, 50);
        }
        public static void main(String[] args) {
                SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                                createAndShowGUI();
                        }
                });
        }
        public static void createAndShowGUI() {
                JFrame w = new JFrame("Game");
                w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                w.setSize(806, 628);
                w.setLocationRelativeTo(null);
                w.setResizable(false);
                w.setVisible(true);
                //THIS IS VERY IMPORTANT. MUST ADD THE PANEL TO THE JFRAME WINDOW.
                GameControl gc = new GameControl();
                w.add(gc);
                gc.requestFocus();
        }
        public GameEngine getEngine() {
        	return engine;
        }
        public GameDisplay getDisplay() {
        	return display;
        }
        public PlayerCharacter getPlayer() {
			return player;
		}
}